﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MyMenu.config;

namespace MyMenuSetup
{
    public partial class Form1 : Form
    {


        private bool recordKey = false;
        public Form1(string path)
        {
            this.configPath = path;
            InitializeComponent();
        }



        private void button1_Click(object sender, EventArgs e)
        {
            //XmlParseur.toXml(this.action, @"c:\MyMenu.xml");
            XmlParseur.toXml(this.action, configPath);
        }



        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            textBox1.Text = e.Node.Text;
            selectedAction = (MyMenu.action.Action)e.Node.Tag;
            selectedNode = e.Node;
            checkBox1.Checked = false;
            if (selectedAction is MyMenu.action.ConfirmedAction)
            {
                MyMenu.action.Action action2 = ((MyMenu.action.ConfirmedAction)selectedAction).action;
                checkBox1.Checked = true;
                if (action2 is MyMenu.action.ExecuteCommand)
                {
                    setExecuteCommand((MyMenu.action.ExecuteCommand)action2);
                }
                if (action2 is MyMenu.action.ActivateWindowCommand)
                {
                    setActivateWindowAction((MyMenu.action.ActivateWindowCommand)action2);
                }
            }
            else if (selectedAction is MyMenu.action.ExecuteCommand)
            {
                setExecuteCommand((MyMenu.action.ExecuteCommand)selectedAction);
            }
            else if (selectedAction is MyMenu.action.ActivateWindowCommand)
            {
                setActivateWindowAction((MyMenu.action.ActivateWindowCommand)selectedAction);
            }
            else
            {
                label2.Visible = false;
                textBox2.Visible = false;
                label3.Visible = false;
                textBox3.Visible = false;
                checkBox1.Visible = false;
                textBox4.Visible = false;
                label4.Visible = false;
                checkBox1.Visible = false;
                Hidden.Visible = false;
            }
        }

        private void setExecuteCommand(MyMenu.action.ExecuteCommand action)
        {
            label2.Visible = true;
            textBox2.Visible = true;
            label3.Visible = true;
            textBox3.Visible = true;
            checkBox1.Visible = true;
            textBox4.Visible = false;
            label4.Visible = false;
            textBox2.Text = action.command;
            textBox3.Text = action.param;
            Hidden.Visible = true;
            Hidden.Checked = action.hidden;
        }

        private void setActivateWindowAction(MyMenu.action.ActivateWindowCommand action)
        {
            label2.Visible = false;
            textBox2.Visible = false;
            label3.Visible = false;
            textBox3.Visible = false;
            checkBox1.Visible = true;
            label4.Visible = true;
            textBox4.Visible = true;
            textBox4.Text = action.id.ToString();
            Hidden.Visible = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (selectedAction != null)
            {
                selectedAction.name = textBox1.Text;
            }
            if (selectedAction != null && selectedAction is MyMenu.action.ExecuteCommand)
            {
                ((MyMenu.action.ExecuteCommand)selectedAction).command = textBox2.Text;
                ((MyMenu.action.ExecuteCommand)selectedAction).param = textBox3.Text;
                ((MyMenu.action.ExecuteCommand)selectedAction).hidden = Hidden.Checked;
                if (checkBox1.Checked)
                {
                    if (selectedNode.Parent != null)
                    {
                        ((MyMenu.action.ShowMenu)selectedNode.Parent.Tag).RemoveAction(selectedAction);
                    }
                    selectedAction = new MyMenu.action.ConfirmedAction(selectedAction);
                    if (selectedNode.Parent != null)
                    {
                        ((MyMenu.action.ShowMenu)selectedNode.Parent.Tag).AddAction(selectedAction);
                    }
                    selectedNode.Tag = selectedAction;
                }
            }
            if (selectedAction != null && selectedAction is MyMenu.action.ActivateWindowCommand)
            {
                ((MyMenu.action.ActivateWindowCommand)selectedAction).id = textBox4.Text;
                if (checkBox1.Checked)
                {
                    if (selectedNode.Parent != null)
                    {
                        ((MyMenu.action.ShowMenu)selectedNode.Parent.Tag).RemoveAction(selectedAction);
                    }
                    selectedAction = new MyMenu.action.ConfirmedAction(selectedAction);
                    if (selectedNode.Parent != null)
                    {
                        ((MyMenu.action.ShowMenu)selectedNode.Parent.Tag).AddAction(selectedAction);
                    }
                    selectedNode.Tag = selectedAction;
                }
            }
            else if (selectedAction != null && selectedAction is MyMenu.action.ConfirmedAction)
            {
                SetAction(((MyMenu.action.ConfirmedAction)selectedAction).action);
                if (!checkBox1.Checked)
                {
                    if (selectedNode.Parent != null)
                    {
                        ((MyMenu.action.ShowMenu)selectedNode.Parent.Tag).RemoveAction(selectedAction);
                    }
                    selectedAction = ((MyMenu.action.ConfirmedAction)selectedAction).action;
                    if (selectedNode.Parent != null)
                    {
                        ((MyMenu.action.ShowMenu)selectedNode.Parent.Tag).AddAction(selectedAction);
                    }
                    selectedNode.Tag = selectedAction;
                }
               
            }
            if (selectedNode != null)
                selectedNode.Text = textBox1.Text;
            treeView1.Update();
        }

        private void SetAction(MyMenu.action.Action action)
        {
            if (action is MyMenu.action.ExecuteCommand)
            {
                setExecuteAction((MyMenu.action.ExecuteCommand)action);
            }
            else if (action is MyMenu.action.ActivateWindowCommand)
            {
                setActivateWindow((MyMenu.action.ActivateWindowCommand)action);
            }
        }

        private void setExecuteAction(MyMenu.action.ExecuteCommand action)
        {
            action.command = textBox2.Text;
            action.param = textBox3.Text;
            action.hidden = Hidden.Checked;
        }

        private void setActivateWindow(MyMenu.action.ActivateWindowCommand action)
        {
            action.id = textBox4.Text;
        }



        private void menuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedAction is MyMenu.action.ShowMenu)
            {

                MyMenu.action.ShowMenu action = new MyMenu.action.ShowMenu();
                action.name = "New Menu";
                TreeNode node = new TreeNode("New Menu");
                node.Tag = action;
                selectedNode.Nodes.Add(node);
                ((MyMenu.action.ShowMenu)selectedAction).AddAction(action);
                treeView1.Update();
            }
            else
            {
                MessageBox.Show("Menu can only be added to Menu");
            }

        }

        private void commandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedAction is MyMenu.action.ShowMenu)
            {
                MyMenu.action.ExecuteCommand action = new MyMenu.action.ExecuteCommand("", "");
                action.name = "New Action";
                TreeNode node = new TreeNode("New Action");
                node.Tag = action;
                selectedNode.Nodes.Add(node);
                ((MyMenu.action.ShowMenu)selectedAction).AddAction(action);
                treeView1.Update();
            }
            else
            {
                MessageBox.Show("Action can only be added to Menu");
            }
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MyMenu.action.Action currentAction = selectedAction;
            if (currentAction == action)
            {
                MessageBox.Show("Cannot remove the first menu");
            }
            else if (currentAction != null)
            {
                MyMenu.action.Action parentAction = (MyMenu.action.Action)selectedNode.Parent.Tag;
                ((MyMenu.action.ShowMenu)parentAction).RemoveAction(currentAction);
                selectedNode.Remove();
                selectedNode = null;
                selectedAction = null;
            }
        }



        private void activateWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (selectedAction is MyMenu.action.ShowMenu)
            {
                MyMenu.action.ActivateWindowCommand action = new MyMenu.action.ActivateWindowCommand("0");
                action.name = "New Action";
                TreeNode node = new TreeNode("New Action");
                node.Tag = action;
                selectedNode.Nodes.Add(node);
                ((MyMenu.action.ShowMenu)selectedAction).AddAction(action);
                treeView1.Update();
            }
            else
            {
                MessageBox.Show("Action can only be added to Menu");
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            XmlParseur.toXml(this.action, configPath);
           
            this.Hide();
            this.Close();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

       

        private void button3_Click(object sender, EventArgs e)
        {
            //MessageBox.Show("Press any key to record");
            recordKey = true;
            this.KeyPreview = true;
            this.KeyPress +=
                new KeyPressEventHandler(Form1_KeyPress);
        }

     
        void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (recordKey)
            {
                MessageBox.Show("KeyPress: " +  (int) e.KeyChar);
                e.Handled = true;
                recordKey = false;
                this.textBox5.Text = "" + (int)  e.KeyChar;
                this.action.keyChar = ((Int32) e.KeyChar).ToString();
            }
          
       
        }

     

    }
}
